package com.moduls.push;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import org.json.JSONObject;

import com.moduls.push.android.AndroidNotification;
import com.moduls.push.android.AndroidUnicast;
import com.moduls.push.ios.IOSUnicast;



public class UPushUtils {
	
	private static String default_file = "push.properties";
	private static String android_appkey = "5bc83ed8b465f5b0e200040e";
	private static String android_appMasterSecret = "fztxmhu220tsvfnspwqlagq9e8p7wnve";
	
	private static String ios_appkey = "5bc84893b465f58c9a00011f";
	private static String ios_appMasterSecret = "xzaxgubpb0i80f3zd6ezrrqndubkn8qo";
	
	private static String android_srv_appkey = "5bc83f01f1f5568f31000390";
	private static String android_srv_appMasterSecret = "ecttiw5aeprv0knm2ijjd6pg3svscuhq";
	
	private static String ios_srv_appkey = "5bc848c5b465f59311000494";
	private static String ios_srv_appMasterSecret = "jgixfhguksbpda1yqjdwy7qayukbsivl";
	
	private static PushClient client = new PushClient();
	private static Properties properties = new Properties();
//	static {
//		try {
//			InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(default_file);
//			properties.load(in);
//			android_appkey = properties.getProperty("android_appkey");
//			android_appMasterSecret = properties.getProperty("android_appMasterSecret");
//			ios_appkey = properties.getProperty("ios_appkey");
//			ios_appMasterSecret = properties.getProperty("ios_appMasterSecret");
//		} catch (IOException ex) {
//			ex.printStackTrace();
//		}
//		
//	}
	

	
	/**
	 * 安卓单播
	 * @param token  设备token
	 * @param ticker 通知栏提示文字
	 * @param title 通知栏标题
	 * @param text 通知栏  文字描述
	 * @throws Exception
	 */
	public static boolean sendAndroidUnicast(String token, String ticker, String title, String text) throws Exception{
		AndroidUnicast unicast = new AndroidUnicast(android_appkey,android_appMasterSecret);
		// TODO Set your device token
		unicast.setDeviceToken(token);
		unicast.setTicker(ticker);
		unicast.setTitle(title);
		unicast.setText(text);
		unicast.goAppAfterOpen();
		unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		unicast.setProductionMode();
		// Set customized fields
		//unicast.setExtraField("test", "helloworld");
		return client.send(unicast);
	}
	
	public static boolean sendAndroidUnicast(int app_type, String token, String ticker, String title, String text, 
			Map<String, Object> extraField) throws Exception{
		AndroidUnicast unicast;
		//客户端
		if(app_type == 1)
		{
			unicast = new AndroidUnicast(android_appkey,android_appMasterSecret);
		}
		else
		{
			unicast = new AndroidUnicast(android_srv_appkey,android_srv_appMasterSecret);
		}
		// TODO Set your device token
		unicast.setDeviceToken(token);
		unicast.setTicker(ticker);
		unicast.setTitle(title);
		unicast.setText(text);
		unicast.goAppAfterOpen();
		unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		unicast.setProductionMode();
		// Set customized fields
		for(Map.Entry<String, Object> entry : extraField.entrySet()) {
			unicast.setExtraField(entry.getKey(), entry.getValue());
		}
		return client.send(unicast);
	}
	
	
	/**
	 * IOS单播
	 * @param token  设备token
	 * @param ticker 通知栏提示文字
	 * @param title 通知栏标题
	 * @param body 通知栏  文字描述
	 * @param customizedField 自定义消息
	 * @throws Exception
	 */
	public static boolean sendIOSUnicast(int app_type, String token,String ticker,String title,String body,
			Map<String,Object> customizedField) throws Exception {
		IOSUnicast unicast;
		//客户端
		if(app_type == 1)
		{
			unicast = new IOSUnicast(ios_appkey,ios_appMasterSecret);
		}
		else//服务端
		{
			unicast = new IOSUnicast(ios_srv_appkey,ios_srv_appMasterSecret);
		}
		// TODO Set your device token
		unicast.setDeviceToken(token);
		
		JSONObject json = new JSONObject();
		json.put("title", ticker);
		json.put("subtitle", title);
		json.put("body", body);
		
		unicast.setAlert(json);
		unicast.setBadge( 1);
		unicast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		unicast.setTestMode();
		// Set customized fields
		
		for(Map.Entry<String, Object> entry : customizedField.entrySet()) {
			unicast.setCustomizedField(entry.getKey(), entry.getValue());
		}
		return client.send(unicast);
	}
	
	public static boolean sendIOSUnicast(String token,String ticker,String title,String body) throws Exception {
		IOSUnicast unicast = new IOSUnicast(ios_appkey,ios_appMasterSecret);
		// TODO Set your device token
		unicast.setDeviceToken(token);
		
		JSONObject json = new JSONObject();
		json.put("title", ticker);
		json.put("subtitle", title);
		json.put("body", body);
		
		unicast.setAlert(json);
		unicast.setBadge( 1);
		unicast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		unicast.setTestMode();
		// Set customized fields
		return client.send(unicast);
	}

}
